package com.itboye.jinpuyiyuan.activity.home;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.BranchListAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.BranchBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;

import com.itboye.jinpuyiyuan.utils.LocationInfoUtil;
import com.itboye.jinpuyiyuan.utils.LocationUtils;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/9 0009.
 */

public class BranchSelectActivity extends BaseActivity implements Observer{
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private ListView branchAddressList;
    private BranchListAdapter branchListAdapter;
    private BranchBean bean;
    private ArrayList<BranchBean.ListInfo> list;
    private Intent intent;
    private String uid;
    private BranchAddressPresenter branchPresenter,presenter;


    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_branch_select);
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("分院选择");
        list=new ArrayList<>();
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");
        branchPresenter=new BranchAddressPresenter(this);
        presenter=new BranchAddressPresenter(this);
        //查询医院
        String lngAndLatWithNetwork = new LocationUtils().getLngAndLat(this);
        presenter.queryBranch(lngAndLatWithNetwork.split(",")[0],lngAndLatWithNetwork.split(",")[1]);//分院查询

        String longitude = LocationInfoUtil.getLongitude(this);
        String getlatitude = LocationInfoUtil.getlatitude(this);
        Log.d("==log==lat",longitude+"==="+getlatitude);
        presenter.updateLocation(uid,getlatitude,longitude,"");

        intent=getIntent();
//        for (int i=0;i<3;i++){
//            BranchBean branchBean = new BranchBean();
//            branchBean.setAddress("地址：辽宁省鞍山市***区");
//            branchBean.setName("鞍山市金普医院");
//            branchBean.setTelephone("12345678901");
//            branchBean.setUrl("https://img02.sogoucdn.com/app/a/200773/b93324be8589bee3fae267dc428248e3");
//            list.add(branchBean);
//        }


        branchAddressList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                list.get(position).getName()
                intent.putExtra("hospitalName",list.get(position).getName().toString());
                intent.putExtra("hosid",list.get(position).getId()+"");
                setResult(121,intent);
                BranchSelectActivity.this.finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        Log.d("======entity",entity.toString());
        if (entity != null) {
            if (entity.getEventType() == BranchAddressPresenter.queryAddress_success) {
//                MAlert.alert("请求成功");

                bean = (BranchBean) entity.getData();
                Log.d("======up",bean.toString());
                list = new ArrayList<BranchBean.ListInfo>();
                list.addAll(bean.getList());
                for (int i=0;i<list.size();i++){
                    list.get(i).setIcon(R.mipmap.ic_launcher);
                }

                branchListAdapter=new BranchListAdapter(this,list);
                branchAddressList.setAdapter(branchListAdapter);

            } else if (entity.getEventType() == BranchAddressPresenter.queryAddress_fail) {
                MAlert.alert(entity.getMsg(), Gravity.TOP);
            }else if (entity.getEventType()== BranchAddressPresenter.By_UserProfile_updateGeoHash_success){

//                MAlert.alert("位置更新成功");
            }else if (entity.getEventType()== BranchAddressPresenter.By_UserProfile_updateGeoHash_fail){
//                MAlert.alert("位置更新失败");
            }
        }

    }
}
